package cn.iocoder.yudao.module.bpm.controller.admin.journalapply;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.bpm.controller.admin.journal.vo.JournalRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.journalapply.vo.JournalApplyPageReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.journalapply.vo.JournalApplyRespVO;
import cn.iocoder.yudao.module.bpm.controller.admin.journalapply.vo.JournalApplySaveReqVO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.journal.JournalDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.journalapply.JournalApplyDO;
import cn.iocoder.yudao.module.bpm.dal.dataobject.journalapplydetail.JournalApplyDetailDO;
import cn.iocoder.yudao.module.bpm.service.journal.JournalService;
import cn.iocoder.yudao.module.bpm.service.journalapply.JournalApplyService;
import cn.iocoder.yudao.module.bpm.service.journalapplydetail.JournalApplyDetailService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 期刊申请")
@RestController
@RequestMapping("/bpm/journal-apply")
@Validated
public class JournalApplyController {

    @Resource
    private JournalApplyService journalApplyService;

    @Resource
    private JournalService journalService;

    @Resource
    private JournalApplyDetailService journalApplyServiceDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建期刊申请")
    @PreAuthorize("@ss.hasPermission('dr:journal-apply:create')")
    public CommonResult<Long> createJournalApply(@Valid @RequestBody JournalApplySaveReqVO createReqVO) {
        return success(journalApplyService.createJournalApply(getLoginUserId(),createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新期刊申请")
    @PreAuthorize("@ss.hasPermission('dr:journal-apply:update')")
    public CommonResult<Boolean> updateJournalApply(@Valid @RequestBody JournalApplySaveReqVO updateReqVO) {
        journalApplyService.updateJournalApply(getLoginUserId(),updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除期刊申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dr:journal-apply:delete')")
    public CommonResult<Boolean> deleteJournalApply(@RequestParam("id") Long id) {
        journalApplyService.deleteJournalApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得期刊申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dr:journal-apply:query')")
    public CommonResult<JournalApplyRespVO> getJournalApply(@RequestParam("id") Long id) {
        JournalApplyDO journalApply = journalApplyService.getJournalApply(id);
        JournalApplyRespVO res = BeanUtils.toBean(journalApply, JournalApplyRespVO.class);
        List<JournalApplyDetailDO> detailList = journalApplyServiceDetailService.getJournalApplyDetailList(id);
        List<JournalDO> detailList1 = journalService.queryByIds(detailList.stream().map(JournalApplyDetailDO::getJournalId).toList());
        res.setJournalDetails(BeanUtils.toBean(detailList1, JournalRespVO.class));
        return success(res);
    }

    @GetMapping("/page")
    @Operation(summary = "获得期刊申请分页")
    @PreAuthorize("@ss.hasPermission('dr:journal-apply:query')")
    public CommonResult<PageResult<JournalApplyRespVO>> getJournalApplyPage(@Valid JournalApplyPageReqVO pageReqVO) {
        PageResult<JournalApplyDO> pageResult = journalApplyService.getJournalApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JournalApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出期刊申请 Excel")
    @PreAuthorize("@ss.hasPermission('dr:journal-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJournalApplyExcel(@Valid JournalApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JournalApplyDO> list = journalApplyService.getJournalApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "期刊申请.xls", "数据", JournalApplyRespVO.class,
                        BeanUtils.toBean(list, JournalApplyRespVO.class));
    }

}